﻿requirejs.config({
    baseUrl: ".",
    paths: {
    	zepto:"javascripts/zepto.min",
    	dingtalk:"javascripts/dingtalk",
        vue: "components/vue/vue.min",
        axios: "components/axios/axios.min",
        MINT: "components/mint-ui/index",
        moment: "components/moment/moment.min",
        search: "resources/js/search",
        datetime: "resources/js/datetime",
        dictionary: "resources/js/dictionary",
        common: "resources/js/common",
        wx: "http://res.wx.qq.com/open/js/jweixin-1.2.0",
        jssdk: "resources/js/jssdk"
    },
    shim: {
        "MINT": {
            deps: ["vue"],
            exports: "MINT"
        },
        "wx": {
            exports: "wx"
        }
    }
});

require(["zepto","dingtalk","vue", "axios", "common", "MINT", "moment", "search", "datetime", "dictionary", "jssdk"], function (zepto,dingtalk,Vue, axios, common, MINT, moment, search, datetime, dictionary, jssdk) {

    Vue.component(MINT.Popup.name, MINT.Popup);
    Vue.component(MINT.Toast.name, MINT.Toast);

    window.MINT = MINT;

    var vm = new Vue({
        el: "#view",
        data: {
            title: "",
            data: {
                incidentType: "故障",
                title: "",
                description: "",
                happenTime: null,
                expectTime: null,
                occurrenceFrequency: "",
                dealType: 2
            },
            others: {
                linkmanName: "",
                userTel: "",
                userMobile: "",
                userEmail: ""
            },
            cusUser: {
                id: "",
                name: ""
            },
            icd_category: {
                id: "",
                name: ""
            },
            supporter: {
                id: "",
                name: ""
            },
            ci: {
                id: "",
                name: ""
            },
            images: [],
            project: []
        },
        mounted: function () {
            var self = this;
        },
        methods: {
            cusUserChange: function () {
                var self = this;

                axios.get("/common/getUserInfo?userInfo=" + self.cusUser.id)
                    .then(function (response) {
                        self.others.linkmanName = response.data.fullname;
                        self.others.userTel = response.data.tel;
                        self.others.userMobile = response.data.mobile;
                        self.others.userEmail = response.data.email;

                        axios.get("/common/project?userId=" + response.data.id)
                            .then(function (projectResponse) {
                                self.project = projectResponse.data;
                                self.data.projectId = "";
                            });
                    });
            },
            startVoice: function (event) {
                event.preventDefault();
                jssdk.startRecord(function (result) {
                    self.data.description = result;
                });
            },
            endVoice: function (event) {
                var self = this;
                event.preventDefault();
                jssdk.stopRecord(function (result) {
                    self.data.description = result;
                });
            },
            imgUpload: function (event) {
                common.imgUpload(event, this.images, MINT.Toast);
            },
            submit: function () {
                if (common.isEmpty(this.cusUser.id)) {
                    MINT.Toast("请输入请求人");
                    return;
                }
                if (common.isEmpty(this.data.title)) {
                    MINT.Toast("请输入标题");
                    return;
                }
                if (common.isEmpty(this.data.description)) {
                    MINT.Toast("请输入事件详细描述");
                    return;
                }
                if (common.isEmpty(this.icd_category.id)) {
                    MINT.Toast("请输入事件分类");
                    return;
                }
                if (common.isEmpty(this.data.urgencyName)) {
                    MINT.Toast("请输入紧急度");
                    return;
                }
                if (common.isEmpty(this.data.effectName)) {
                    MINT.Toast("请输入影响度");
                    return;
                }
                if (common.isEmpty(this.data.priorityName)) {
                    MINT.Toast("请输入优先级");
                    return;
                }
                if (this.data.dealType == 1) {
                    if (common.isEmpty(this.data.reasonAnalyze)) {
                        MINT.Toast("请输入原因分析");
                        return;
                    }
                    if (common.isEmpty(this.data.handleProcess)) {
                        MINT.Toast("请输入处理过程");
                        return;
                    }
                    if (common.isEmpty(this.data.solution)) {
                        MINT.Toast("请输入解决方案");
                        return;
                    }
                }
                if (this.data.dealType == 2) {
                    if (common.isEmpty(this.supporter.id)) {
                        MINT.Toast("请输入支持人员");
                        return;
                    }
                }

                var data = {
                    data: this.data,
                    others: this.others,
                    images: this.images
                };
                data.data.cusUserId = this.cusUser.id;
                data.data.icdCategoryId = this.icd_category.id;
                data.data.icdCategoryName = this.icd_category.name;
                data.data.ciId = this.ci.id;
                data.data.ciCode = this.ci.id;
                if (this.data.dealType == 2) {
                    data.data.supporterId = this.supporter.id;
                    data.data.supporterName = this.supporter.name;
                }

                data.others.longitude = jssdk.location().longitude.toString();
                data.others.latitude = jssdk.location().latitude.toString();

                axios.post("/incident/new", data)
                    .then(function (response) {
                        alert("已报单");
                        common.goback();
                    });
            }
        }
    });
});